function zdata = zigzag(data)
    [M, N] = size(data);
    if (numel(data) ~= 64) || ((N ~= 8) && (N ~= 1) && (N ~= 64))
        error('Size of DATA should be 8x8, 1x64 or 64x1.');
    end
%      1     9    17    25    33    41    49    57
%      2    10    18    26    34    42    50    58
%      3    11    19    27    35    43    51    59
%      4    12    20    28    36    44    52    60
%      5    13    21    29    37    45    53    61
%      6    14    22    30    38    46    54    62
%      7    15    23    31    39    47    55    63
%      8    16    24    32    40    48    56    64
    if N ~= 1
        data = data(:);
    end
    TABLE = [1,9,2,3,10,17,25,18,11,4,5,12,19,26,33,41,34,27,20,13,6,...
        7,14,21,28,35,42,49,57,50,43,36,29,22,15,8,16,23,30,37,44,51,58,...
        59,52,45,38,31,24,32,39,46,53,60,61,54,47,40,48,55,62,63,56,64];
    zdata = data(TABLE);
    %zdata = reshape(zdata, M, N);
end
